<!-- 新增和修改 -->
<template>
  <div class="app-container">
    <Edialog
      :outershow="outershow"
      :title="title"
      @saveFn="saveFn"
      @close="closeFn"
      :saveClick="true"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="模板编码" prop="templateCode" v-if="type == 'add'">
          <el-input v-model="form.templateCode" />
        </el-form-item>
        <el-form-item label="模板编码" prop="templateCode" v-else>
          <div>{{ form.templateCode }}</div>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" />
        </el-form-item>

        <el-form-item label="备注" prop="remark" v-if="type == 'add'">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
    </Edialog>
  </div>
</template>

<script>
import Edialog from "@/components/Edialog";
import { putBsTemplateApi, postBsTemplateApi } from "@/api/templateManage";
export default {
  data() {
    return {
      type: "",
      outershow: false,
      title: "新增",
      form: {
        templateCode: "",
        templateName: "",
        remark: "",
      },
      rules: {
        templateCode: [
          { required: true, message: "请输入模板编码", trigger: "blur" },
        ],
        templateName: [
          { required: true, message: "请输入模板名称", trigger: "change" },
        ],
      },
    };
  },
  components: {
    Edialog,
  },
  methods: {
    //外部打开弹窗
    openFn(type, row) {
      console.log(row);
      this.type = type;
      this.outershow = true;
      if (type === "edit") {
        this.form = row;
        this.title = "修改";
      } else {
        this.form = {
          templateCode: "",
          templateName: "",
          remark: "",
        };
        setTimeout(() => {
          this.$nextTick(() => {
            this.$refs.form.resetFields();
          });
        });

        this.title = "新增";
      }
    },
    saveFn() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type == "edit") {
            postBsTemplateApi(this.form).then((res) => {
              if (res.data && res.data.bcode == 0) {
                this.$message.success(res.data.bmsg || "修改成功");
                this.closeFn();
                this.$emit("refreshFn");
              }
            });
          } else {
            putBsTemplateApi(this.form).then((res) => {
              if (res.data && res.data.bcode == 0) {
                this.$message.success(res.data.bmsg || "新增成功");
                this.closeFn();
                this.$emit("refreshFn");
              }
            });
          }
        }
      });
    },
    closeFn() {
      this.$refs.form.resetFields();
      this.outershow = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
